'use strict';

const Service = require('egg').Service;

class CarService extends Service {
    //添加
    async add(uid,pid,num) {
       await this.app.mysql.query('insert into carlist (uid,pid,num) values (?,?,?)',[uid,pid,num]) 
    }
    //查询
    async select(uid,pid){
        return await this.app.mysql.query('select * from carlist where uid=? and pid=?',[uid,pid])
    }
    //更新
    async update(uid,pid,num){
        await this.app.mysql.query('update carlist set num=? where uid=? and pid=?',[num,uid,pid])
    }

    async carUpdate(id,num){
        await this.app.mysql.query('update carlist set num=? where id=?',[num,id])
    }

    //carlist
    async carlist(uid){
        let buylist = await this.app.mysql.query('select * from carlist where uid=?',[uid]);
        let pids = [];
        buylist.forEach(item => {
            pids.push(item.pid);
        })
        let carlist = await this.app.mysql.query(`select * from productlist where id in ('${pids}')`);
        console.log("===",carlist);
        carlist = carlist.map((item,index) => {
            return {
                ...item,
                num:buylist[index].num
            }
        })
        return carlist
    }
}

module.exports = CarService;
